const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mobile: '',
        code: '',
        is_send: false,
        time: 60,
        wxcode: '',
        avatar: '',
        nickname: '',
        sex: ''
    },

    setMobile: function(e){
        this.setData({
            mobile: e.detail.value
        })
    },

    setCode: function(e){
        this.setData({
            code: e.detail.value
        })
    },

    send: function(){
        if(this.data.is_send) return false;
        if(!this.data.mobile){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none'
            });
            return false;
        }
        app.ajax({
            url: '/api/index/code',
            data: {
                mobile: this.data.mobile
            },
            success: res => {
                if(res.code == 1){
                    var time = 60, timer;
                    this.setData({
                        is_send: true
                    })
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                    timer = setInterval(() => {
                        time--;
                        this.setData({
                            time: time
                        })
                        if(time <= 0){
                            clearInterval(timer);
                            this.setData({
                                is_send: false
                            })
                        }
                    }, 1000);
                }else{
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                }
            }
        }, 1);
    },

    loginMobile: function (e) {
        console.log(e);
        if(!this.data.mobile){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none'
            });
            return false;
        }
        if(!this.data.code){
            wx.showToast({
                title: '请输入验证码',
                icon: 'none'
            });
            return false;
        }
        app.ajax({
            url: '/api/user/loginMobile',
            data: {
                mobile: this.data.mobile,
                code: this.data.code,
                avatar: this.data.avatar,
                nickname: this.data.nickname,
                sex: this.data.sex
            },
            success: res => {
                if(res.code == 1){
                    wx.navigateBack({
                        detal: 1
                    })
                }else{
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                }
            }
        }, 1)
    },

    loginWx: function (e) {
        console.log(e);
        console.log(e.detail.iv);
        if(!e.detail.iv){
            wx.showToast({
                title: '未获取到手机号',
                icon: 'none'
            });
            return false;
        }
        console.log(this.data);
        app.ajax({
            url: '/api/user/loginWxa',
            data: {
                code: this.data.wxcode,
                iv: e.detail.iv,
                encryptedData: e.detail.encryptedData,
                avatar: this.data.avatar,
                nickname: this.data.nickname,
                sex: this.data.gender
            },
            success: res => {
                if(res.code == 1){
                    wx.navigateBack({
                        detal: 1
                    })
                }else{
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                    wx.login({
                        success: res => {
                            this.setData({
                                wxcode: res.code
                            });
                        }
                    });
                }
            }
        }, 1)
    },

    goHome: function(){
        wx.redirectTo({
            url: '/pages/index/index',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var userinfo = wx.getStorageSync('userinfo');
        console.log(userinfo)
        if(userinfo){
            this.setData({
                avatar: userinfo.avatarUrl,
                nickname: userinfo.nickName,
                gender: userinfo.gender
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      console.log("onShow");
        wx.login({
            success: res => {
                this.setData({
                    wxcode: res.code
                });
            }
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})